﻿
Public Class RetestWizardExt
    Private aSamplePL As SystemBoxSDK.PL.Void_PL
    Private aSampleDetailPL As SystemBoxSDK.PL.Void_PL
    Private WithEvents gridSample As GridExtPL
    Private WithEvents gridSampleDetail As GridExtPL
    Private selectedSampleRow As DataRow
    Private selectedSampleID As String
    Private sampleDetailList As Dictionary(Of String, String) = New Dictionary(Of String, String)


    Private Sub WizardControl1_CancelClick(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WizardControl1.CancelClick
        If MessageBox.Show("Do you really want to exit ?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If

    End Sub

    Private Sub WizardControl1_FinishClick(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WizardControl1.FinishClick
        Me.Close()
    End Sub


    ''' <summary>
    ''' This sub initialize each step depends on witch page the wizard is on
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub WizardControl1_SelectedPageChanged(sender As Object, e As DevExpress.XtraWizard.WizardPageChangedEventArgs) Handles WizardControl1.SelectedPageChanged
        Select Case e.Page.Name
            Case "SelectSample"
                If aSamplePL Is Nothing Then

                    aSamplePL = New SystemBoxSDK.PL.Void_PL("LOV_Retest_Sample")
                    gridSample = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aSamplePL}
                    gridSample.Name = "RetestSample"
                    gridSample.AllowCreate = False
                    gridSample.AllowDelete = False
                    gridSample.AllowUpdate = False

                    AddHandler gridSample.MasterRowClick, AddressOf SampleRowClick

                    e.Page.Controls.Add(gridSample)
                End If

            Case "SelectSampleDetail"
                If selectedSampleRow IsNot Nothing AndAlso selectedSampleRow("QAQCInspLotID").ToString() <> selectedSampleID Then
                    ClearSummary()

                    selectedSampleID = selectedSampleRow("QAQCInspLotID").ToString()
                    aSampleDetailPL = New SystemBoxSDK.PL.Void_PL("LOV_Retest_SampleDetail")
                    aSampleDetailPL.SQLWhere = String.Format("QAQCInspLotID_FK = '{0}'", selectedSampleID)
                    gridSampleDetail = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aSampleDetailPL, .LoadWithChilds = False, .SecureGrid = False}
                    gridSampleDetail.Name = "RetestSampleDetail"
                    gridSampleDetail.AllowCreate = False
                    gridSampleDetail.AllowDelete = False


                    AddHandler gridSampleDetail.MasterCellValueChanged, AddressOf SampleDetailCellValueChanged

                    If e.Page.Controls("RetestSampleDetail") IsNot Nothing Then
                        e.Page.Controls.Remove(e.Page.Controls("RetestSampleDetail"))
                    End If

                    e.Page.Controls.Add(gridSampleDetail)
                End If

            Case "SummaryPage"
                Dim dt_utilisation As Date = dtAnalysis.DateTime
                Dim SampleDate As Date = dtSample.DateTime
                Dim i As Integer = sampleDetailList.Count

                Dim pair As KeyValuePair(Of String, String)
                listboxSelectedSampleDetail.Items.Clear()
                For Each pair In sampleDetailList
                    listboxSelectedSampleDetail.Items.Add(pair.Value)
                Next


                lblLotSerialNo.Text = selectedSampleRow("LotSerialNo").ToString()
                lblGroup.Text = selectedSampleRow("Value_ALT").ToString()
                lblAnalysisDate.Text = dt_utilisation.Date.ToString()
                lblSampleDate.Text = SampleDate.Date.ToString()

            Case "ProgessPage"
                Dim str As String = String.Empty
                For Each item As KeyValuePair(Of String, String) In sampleDetailList
                    str = str + item.Key + ","
                Next
                CreateRetest(str)

        End Select
    End Sub

    ''' <summary>
    ''' This sub save the selected Sample everytime it changes so it can be use to filter Sample's Details in the next step
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub SampleRowClick(ByVal sender As Object, ByVal e As RowClickEventArgs)
        Dim view As GridView = CType(sender, GridView)
        selectedSampleRow = view.GetFocusedDataRow()
    End Sub

    ''' <summary>
    ''' This sub add/remove from a list, witch will be used to save the desired test
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub SampleDetailCellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        Dim view As GridView = CType(sender, GridView)
        Dim aRow As DataRow = view.GetFocusedDataRow()

        If e.Column.FieldName = "Selected" Then
            If CBool(e.Value.ToString()) = True Then
                If sampleDetailList.ContainsKey(aRow("InspLotResultID").ToString()) = False Then
                    sampleDetailList.Add(aRow("InspLotResultID").ToString(), aRow("SpecDetDescr_ALT").ToString())
                End If
            ElseIf CBool(e.Value.ToString()) = False Then
                If sampleDetailList.ContainsKey(aRow("InspLotResultID").ToString()) = True Then
                    sampleDetailList.Remove(aRow("InspLotResultID").ToString())
                End If
            End If
        End If
    End Sub

    ''' <summary>
    ''' This sub clears all summary informations
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ClearSummary()
        sampleDetailList.Clear()
        listboxSelectedSampleDetail.Items.Clear()
        lblLotSerialNo.Text = String.Empty
        lblGroup.Text = String.Empty
        lblAnalysisDate.Text = String.Empty
        lblSampleDate.Text = String.Empty
    End Sub

    ''' <summary>
    ''' This sub called the stored procedure that will create a retest
    ''' </summary>
    ''' <param name="stringOfInspLotResultID"></param>
    ''' <remarks></remarks>
    Private Sub CreateRetest(ByVal stringOfInspLotResultID As String)
        Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

        Try
            sqlcmd = MetaQuery.GetSPCmdByMetaName("Retest_Create")
            sqlcmd.Parameters.Clear()
            sqlcmd.Parameters.AddWithValue("@QAQCInspLotID", CType(selectedSampleRow("QAQCInspLotID"), Guid))
            sqlcmd.Parameters.AddWithValue("@StringOfInspLotResultID", stringOfInspLotResultID)
            sqlcmd.Parameters.AddWithValue("@Dt_Utilisation", dtAnalysis.DateTime)
            sqlcmd.Parameters.AddWithValue("@SampleDT", dtSample.DateTime)

            sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)

        Catch sqlex As System.Data.SqlClient.SqlException
            MessageWindow.ShowMessage(sqlex)
        Finally
            If sqlcmd IsNot Nothing Then
                sqlcmd.Dispose()
            End If
        End Try
    End Sub
   
End Class